package cn.nawang.ebeim.server.util;

import cn.nawang.ebeim.client.transfer.Client;
import cn.nawang.ebeim.common.entity.FileInfo;
import cn.nawang.ebeim.server.constants.Config;
import cn.nawang.ebeim.server.entity.StorageServer;
import cn.nawang.ebeim.server.service.ZookeeperService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.List;

/**
 * @ClassName: TransportUtil
 * @Description: TODO
 * @author fanyb
 * @date Sep 15, 2015 10:38:00 AM
 * @version v1.0
 */
public class TransportUtil {
  private static final Logger LOG = LoggerFactory.getLogger(TransportUtil.class);

  /**
   * 
   * @author fanyb
   * @Description: 获取要下载的云存储
   * @param storageCloudId
   * @return StorageServer
   * @date Sep 15, 201511:01:29 AM
   *
   * @修改人
   * @修改日期
   */
  public static StorageServer getPreferredServer(String storageCloudId) {
    List<StorageServer> servers = ZookeeperService.instance().getStorageServers(storageCloudId);
    if (servers != null && servers.size() > 0) {
      return servers.get(0);
    }
    return null;
  }

  /**
   * 
   * @author fanyb
   * @Description: 下载版本树文件
   * @param dsId
   * @param version
   * @param storageCloudId
   * @date Sep 16, 20153:41:07 PM
   *
   * @修改人
   * @修改日期
   */
  public static void downloadVersionTree(String dsId, String version,String storageCloudId) {
    LOG.debug("TODO - downloadVersionTree:{}", version);
    StorageServer server = getPreferredServer(storageCloudId);
    if (server == null) {
      LOG.error("no storage server to connect to");
      return;
    }
    Client client = new Client(server.getHost(), server.getPort());
    FileInfo info = new FileInfo();
    info.setType(FileInfo.TYPE_VERSION_DOWNLOAD);
    info.setDataId(dsId);
    info.setVersion(version);
    try {
      client.doTask(info);
    } catch (Exception e) {
      e.printStackTrace();
      LOG.debug("TODO - downloadVersionTreeToDo Fail{}");
      String fileDir=PathUtil.getVersionTreeLocalBreakUpLoad(dsId);
      String fileName=fileDir+File.separator+Config.TEMP_FILE_PREFIX+version;
      String breakFileName=fileDir+File.separator+Config.BREAK_POINT_PREFIX+version;
      File file=new File(fileName);
      File breakFile=new File(breakFileName);
      boolean renametoFlag=file.renameTo(breakFile);
      LOG.debug("break point renameTo result:"+renametoFlag);
      LOG.debug("update downloadVersionTreeToDo over");
      
    }
  }

  /**
   * 
   * @author fanyb
   * @Description: 下载数据文件
   * @param dsId
   * @param signature
   * @param storageCloudId
   * @return boolean
   * @date Sep 16, 20153:41:39 PM
   *
   * @修改人
   * @修改日期
   */
  public static void downloadDataFile(String dsId, String signature,String storageCloudId) {
    LOG.debug("TODO - downloadDataFile:{}", signature);
    StorageServer server = getPreferredServer(storageCloudId);
    if (server == null) {
      LOG.error("no storage server to connect to");
      return ;
    }
    Client client = new Client(server.getHost(), server.getPort());
    FileInfo info = new FileInfo();
    info.setType(FileInfo.TYPE_FILE_DOWNLOAD);
    info.setDataId(dsId);
    info.setSignature(signature);
    try {
      client.doTask(info);
//      return true;
    } catch (Exception e) {
      e.printStackTrace();
      LOG.debug("TODO - downloadFileToDo Fail{}");
      String fileDir=PathUtil.getFileUploadBreakUpload(dsId);
      String fileName=fileDir+File.separator+Config.TEMP_FILE_PREFIX+signature;
      String breakFileName=fileDir+File.separator+Config.BREAK_POINT_PREFIX+signature;
      File file=new File(fileName);
      File breakFile=new File(breakFileName);
      boolean renametoFlag=file.renameTo(breakFile);
      LOG.debug("break point renameTo result:"+renametoFlag);
      LOG.debug("update storageFileToDo over");
//      return false;
    }
  }
}
